package com.kongjian.manage.utils;

import com.alibaba.fastjson2.JSON;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class Sm4Util {

    /**
     * 调用接口获取SM4加密数据
     * @param password
     * @return
     */
    public static String sm4Password(String password) {

        String sm4pw = "L+nnj/XqGZ9q8Cc+gkJyLQ==";

        String url = "http://10.82.118.241:9090/api/crypto/sysEncrypt";

        /**
         * {
         *     "algorithmType":"sm4",
         *     "data":"123456",
         *     "iv":"MTIzNDU2Nzg5MDEyMzQ1Ng==",
         *     "key":"L+nnj/XqGZ9q8Cc+gkJyLQ==",
         *     "padMode":"PKCS5Padding",
         *     "plainIsEncode":"false"
         * }
         */
        HashMap<String, Object> map = new HashMap<>();
        map.put("algorithmType", "sm4");
        map.put("data", password);
        map.put("encMode", "cbc");
        map.put("iv", Base64.getEncoder().encodeToString("1234567890123456".getBytes()));
        map.put("key", sm4pw);
        map.put("padMode", "PKCS5Padding");
        map.put("plainIsEncode", "false");

        ResponseEntity<String> stringResponseEntity = new RestTemplate().postForEntity(url, map, String.class);
        String body = stringResponseEntity.getBody();
        Map maps = (Map) JSON.parse(body);

        String data = maps.get("data").toString();
        return data;
    }



}
